package com.ydj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

public class ModifyRuoyiVuePlusCloud {
    static String sourcePath="D:\\git\\RuoYi-Cloud-Plus";
    static String renameTo="D:\\git\\xingfaBaseCloud";
    
    static String dirName="xingfa-cloud";
    static String projectName="xingfa-base-cloud";
    static String oldPackageName="com.ruoyi";
    static String oldPackagePath="com\\ruoyi";

//    static String oldPackageName="org.dromara";
//    static String oldPackagePath="org\\dromara";
    
    static String packageName="com.xingfa.cloud";
    static String packagePath="com\\xingfa\\cloud";
    
    static String artificatName="xingfa-cloud";
    static String groupName="com.xingfa.cloud";
    static String siteName="兴发基础件";
    static String author ="xf109633";
    
    static Map<String,String> fileNameMap=new LinkedHashMap<>();
    
    static Map<String,String> replaceMap=new LinkedHashMap<>();
    
    static {
        replaceMap.put("ruoyi-cloud-plus", projectName);  //替换目录中和pom文件中
        replaceMap.put(oldPackageName, packageName);       //替换 pom文件中的 group
        replaceMap.put("ruoyi-", artificatName+"-");      //替换 pom文件中的 arificat
        replaceMap.put("/ruoyi/", "/xingfa/");            //修改替换目录夹
        replaceMap.put("@author ruoyi", "@author xf109633"); //替换作者
        replaceMap.put("@author Lion Li", "@author xf109633");//替换作者
        replaceMap.put("RuoYi-Vue-Plus启动成功","兴发基础框架启动成功");//替换启动信息
        replaceMap.put("RuoYi-Cloud-Plus","XingfaBaseCloud");//替换 启动信息和配置文件中的RuoYi
        replaceMap.put("RuoYi", "XingfaBase");               //替换类文件名
        fileNameMap.put("RuoYi", "XingfaBase");
        replaceMap.put("ruoyi.name", "xingfabase.name");    //替换类里的包名
        replaceMap.put("ruoyi:", "xingfabase:");            //替换配置ymal文件中
        replaceMap.put("prefix = \"ruoyi\"", "prefix = \"xingfabase\"");//替换 配置类中 前缀
        replaceMap.put("ruoyiConfig", "xingfaBaseConfig"); //替换 配置
        replaceMap.put("name: ruoyi", "name: xingfaBase"); // 替换配置文件中的配置       
    }
    static List<String> excludePath=new ArrayList<>();
    static {
        excludePath.add("node_modules");
    }
//    static String diffPackage="";
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File f=new File(renameTo);
        try {
            FileUtils.cleanDirectory(new File(renameTo));
            FileUtils.copyDirectory(new File(sourcePath), f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 
        changeFilename(f);
    }


    private static void changeFilename(File f) {
        String fileName=f.getName();
        if(excludePath.size()>0) {
            for(String tmpPath:excludePath) {
                if(fileName.indexOf(tmpPath)>-1) {
                    return;
                }
            }
        }
        if(fileName.indexOf("ruoyi-")>-1) {
            File tmpFile=new File(f.getParentFile().getPath()+File.separator+f.getName().replace("ruoyi-", dirName+"-"));
            try {
                if(f.isDirectory()) {
                    FileUtils.copyDirectory(f, tmpFile);
                    FileUtils.deleteDirectory(f);
                    f=tmpFile;
                }else {
                    FileUtils.copyFile(f, tmpFile);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(f.isDirectory()) {
            for(File ff: f.listFiles()) {
                changeFilename(ff);
            }
            System.out.println(f.getPath());
            if(f.getPath().endsWith(oldPackagePath)) {
                File javaFile= new File(f.getPath().substring(0,f.getPath().indexOf(oldPackagePath)));
                String tmpFile = javaFile.getParent()+"\\tmp";
                String newFile=javaFile.getPath()+"\\"+packagePath;
                try {
                    Files.move(Paths.get(f.getPath()), Paths.get(tmpFile), StandardCopyOption.REPLACE_EXISTING);
                    FileUtils.cleanDirectory(javaFile);
                    FileUtils.copyDirectory(new File(tmpFile), new File(newFile));
                    FileUtils.deleteDirectory(new File(tmpFile));
                    f=new File(newFile);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }else {
            // 打开文件替换里面的 内容
            // 替换文件名
            if(f.getPath().indexOf("RuoYi")>0) {
                File newFile = new File(f.getPath().replaceAll("RuoYi",fileNameMap.get("RuoYi")));
                try {
                    FileUtils.copyFile(f, newFile);
                    FileUtils.forceDelete(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                f=newFile;
            }
            replaceContent(f.getPath(),replaceMap);
        }
        
//        File[] files=f.listFiles();
//        for(file)
//
        
    }

    private static void replaceContent(String filePath, Map<String,String> replaceMap) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            StringBuilder content = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                for(String key:replaceMap.keySet()) {
                    line=line.replaceAll(key, replaceMap.get(key));
                }
                content.append(line).append(System.lineSeparator());
            }
            reader.close();

            FileWriter writer = new FileWriter(filePath);
            writer.write(content.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
}
